import { _decorator, Component, Node, SystemEvent, Touch, EventTouch } from 'cc';
import { GameManager } from '../framework/GameManager';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = UIMain
 * DateTime = Fri Jan 07 2022 20:02:57 GMT+0800 (中国标准时间)
 * Author = Kesa
 * FileBasename = UIMain.ts
 * FileBasenameNoExtension = UIMain
 * URL = db://assets/Scripts/ui/UIMain.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */

@ccclass('UIMain')
export class UIMain extends Component {
    // 触摸速度
    @property
    public speed = 2;

    // 一个触摸转像素的常数
    public constant = 0.01;

    // 获得player飞机节点
    @property(Node)
    public playerPlane: Node = null;

    // 获得 GameManager 节点
    @property(GameManager)
    public gameManager: GameManager = null;

    onEnable() {
        // 运动
        this.node.on(SystemEvent.EventType.TOUCH_MOVE, this.__touch_move, this);

        // 射击
        this.node.on(SystemEvent.EventType.TOUCH_START, this.__touch_start, this);
        this.node.on(SystemEvent.EventType.TOUCH_END, this.__touch_end, this);
    }

    onDisable() {
        this.node.off(SystemEvent.EventType.TOUCH_MOVE, this.__touch_move, this);
    }

    start() {}

    // -----------------------------------

    /**
     *  触摸移动飞机
     * @param touch
     * @param event
     */
    __touch_move(touch: Touch, event: EventTouch) {
        const delta = touch.getDelta();
        let pos = this.playerPlane.position;
        this.playerPlane.setPosition(pos.x + this.constant * this.speed * delta.x, pos.y, pos.z - this.constant * this.speed * delta.y);
    }

    /**
     *  开始触摸 -> 开始射击
     */
    __touch_start(touch: Touch, event: EventTouch) {
        this.gameManager.isShooting(true);
    }
    /**
     *  结束触摸 -> 结束射击
     */
    __touch_end(touch: Touch, event: EventTouch) {
        this.gameManager.isShooting(false);
    }
}
